const { get_fst_file, get_full_path } = require("../utils/file_tool");
const { getByteSize } = require("../utils/transform_tool");
const { BaseCommand } = require("./base");

class SplitFileCommand extends BaseCommand {
    async execute() {
        const splitFile = require("split-file");
        let { src, maxSize, numParts, dst } = this.selfData;
        src = get_fst_file(src);
        dst = get_full_path(dst, "FOLDER");
        const names = await new Promise((resolve, reject) => {
            if (maxSize) {
                maxSize = getByteSize(maxSize);
                splitFile
                    .splitFileBySize(src, maxSize, dst)
                    .then((names) => {
                        resolve(names);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            } else if (numParts) {
                splitFile
                    .splitFile(src, numParts, dst)
                    .then((names) => {
                        resolve(names);
                    })
                    .catch((err) => {
                        reject(err);
                    });
            } else {
                reject("maxSize or numParts is required");
            }
        });
        return names;
    }
}

module.exports = {
    SplitFileCommand,
};
